package com.qingcheng.service.goods.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qingcheng.entity.PageResult;
import com.qingcheng.entity.Result;
import com.qingcheng.mapper.BrandMapper;
import com.qingcheng.pojo.goods.Brand;
import com.qingcheng.service.goods.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/*
 *@Parm
 *@ClassName BrandSerciveImpl
 *@Date 2022/1/8 18:59
 */
@Service
public class BrandSerciveImpl extends ServiceImpl<BrandMapper, Brand> implements BrandService {
    @Autowired
    private BrandMapper brandMapper;

    @Override
    public void add(Brand brand) {
        brandMapper.insert(brand);
    }

    @Override
    public List<Brand> findAll() {
        List<Brand> brands = brandMapper.selectList(null);
        return brands;
    }

    public Result findPage(int page, int size) {
        Page<Brand> p = new Page<>(page, size);
        Page<Brand> brandPage = brandMapper.selectPage(p, null);
        PageResult<Brand> PageResult = new PageResult<>(brandPage.getTotal(), brandPage.getRecords());
        return new Result(0, true, "success", PageResult);
    }

    @Override
    public Result searchPage(Map<String, Object> searchMap, int page, int size) {
        Page<Brand> p = new Page<>(page, size);
        LambdaQueryWrapper<Brand> wrapper = new LambdaQueryWrapper<>();
        if (searchMap.size() != 0) {
            Set<String> set = searchMap.keySet();
            for (String s : set) {
                if (s.equals("name")) {
                    wrapper.like(Brand::getName, searchMap.get(s));
                }
                if (s.equals("id")) {
                    wrapper.eq(Brand::getId, searchMap.get(s));
                }
                if (s.equals("letter")) {
                    wrapper.eq(Brand::getLetter, searchMap.get(s));
                }
            }
        }
        Page<Brand> brandPage = brandMapper.selectPage(p, wrapper);
        return new Result(0, true, "success", brandPage.getRecords());

    }
    
    @Override
    public Result updateBy(Brand brand) {
        int i = brandMapper.updateById(brand);
        if (i<=0){
            return new Result(1,false,"修改失败");
        }
        return new Result(0,true,"修改完成");
    }

    @Override
    public Result deleteBy(Integer id) {
        brandMapper.deleteById(id);
        return new Result(0,true,"删除完成");
    }
}
